namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorSlowStochasticOscillator : BaseIndicator
    {
        private string fastkField = ("SLOWKD.FastkField" + Common.GetGuid());
        private string rsvField = ("SLOWKD.RsvField" + Common.GetGuid());
        private LineShape tlsD;
        private LineShape tlsK;

        public IndicatorSlowStochasticOscillator()
        {
            base.IndType = IndicatorType.SLOWKD;
            base.IndName = "SLOWKD";
            base.IndDesc = "慢速随机指标";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 9.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "M1", 3.0));
            base.IndPeriod.Add(new IndicatorPeriod(2, "M2", 3.0));
            base.IndPeriod.Add(new IndicatorPeriod(3, "M3", 3.0));
            base.SourceDic["Close"] = "";
            base.SourceDic["High"] = "";
            base.SourceDic["Low"] = "";
            base.fields = new string[] { "K", "D" };
        }

        public override bool Contains(object obj)
        {
            if ((obj != this.tlsK) && (obj != this.tlsD))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            double curValue = Common.CalculateRawStochasticValue(r, (int) base.IndPeriod[0].ParamValue, base.SourceDic["Close"], base.SourceDic["High"], base.SourceDic["Low"], base.Crab.DataSource);
            double num2 = Common.CalcuteSimpleMovingAvg(r, (int) base.IndPeriod[1].ParamValue, 1, this.fastkField, this.rsvField, curValue, base.Crab.DataSource);
            double num3 = Common.CalcuteSimpleMovingAvg(r, (int) base.IndPeriod[2].ParamValue, 1, this.tlsK.Name, this.fastkField, num2, base.Crab.DataSource);
            double num4 = Common.CalcuteSimpleMovingAvg(r, (int) base.IndPeriod[3].ParamValue, 1, this.tlsD.Name, this.tlsK.Name, num3, base.Crab.DataSource);
            return new double[] { curValue, num2, num3, num4 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsK, this.tlsD };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsK);
            base.Crab.DeleteShape(this.TlsD);
            base.Crab.DataSource.Columns.Remove(this.RsvField);
            base.Crab.DataSource.Columns.Remove(this.FastkField);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.RsvField, numArray[0], r);
                        base.Crab.SetValue(this.FastkField, numArray[1], r);
                        base.Crab.SetValue(this.TlsK.Name, numArray[2], r);
                        base.Crab.SetValue(this.TlsD.Name, numArray[3], r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            this.TlsK = base.Crab.AddLine(fields[0], panelID);
            this.TlsD = base.Crab.AddLine(fields[1], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            base.Crab.SetLine(fields[1], fields[1], Color.Yellow, 1, 0.01, 2, DashStyle.Solid);
            base.IndPeriod[0].ParamValue = periods[0];
            base.IndPeriod[1].ParamValue = periods[1];
            base.IndPeriod[2].ParamValue = periods[2];
            base.IndPeriod[3].ParamValue = periods[3];
            base.SourceDic["Close"] = source[0];
            base.SourceDic["High"] = source[1];
            base.SourceDic["Low"] = source[2];
            base.Crab.DataSource.Columns.Add(this.RsvField);
            base.Crab.DataSource.Columns.Add(this.FastkField);
        }

        public string FastkField
        {
            get
            {
                return this.fastkField;
            }
            set
            {
                this.fastkField = value;
            }
        }

        public string RsvField
        {
            get
            {
                return this.rsvField;
            }
            set
            {
                this.rsvField = value;
            }
        }

        public LineShape TlsD
        {
            get
            {
                return this.tlsD;
            }
            set
            {
                this.tlsD = value;
            }
        }

        public LineShape TlsK
        {
            get
            {
                return this.tlsK;
            }
            set
            {
                this.tlsK = value;
            }
        }
    }
}

